package com.develop.web.in.service;

import com.develop.data.base.model.IdSpaceTimeRecord;
import com.develop.data.loan.model.LoanContract;
import com.develop.data.loan.repository.LoanContractRepository;
import com.develop.web.in.service.api.LoanContractCreateService;
import com.develop.web.in.service.api.LoanContractDeleteService;
import com.develop.web.in.service.api.LoanContractDetailService;
import com.develop.web.in.service.api.LoanContractUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component("LoanContractJpaService")
public class LoanContractJpaService implements
        LoanContractCreateService<LoanContract, LoanContract>
        , LoanContractDeleteService<LoanContract, LoanContract>
        , LoanContractUpdateService<LoanContract, LoanContract>
        , LoanContractDetailService<LoanContract, LoanContract> {
    @Autowired
    public LoanContractRepository loanContractRepository;

    @Override
    public List<LoanContract> batchCreate(List<LoanContract> items){
        return loanContractRepository.saveAll(items);
    }

    @Override
    public LoanContract batchCreate(LoanContract item){
        return loanContractRepository.save(item);
    }

    @Override
    public List<LoanContract> detailAll(){
        return loanContractRepository.findAll();
    }


    @Override
    public List<LoanContract> batchDetail(List<LoanContract> items){
        List<String> ids = items.stream().map(IdSpaceTimeRecord::getId).collect(Collectors.toList());
        return loanContractRepository.findAllById(ids);
    }

    @Override
    public LoanContract detail(LoanContract item){
        return loanContractRepository.findById(item.getId()).orElse(null);
    }


}
